#include <rtthread.h>
#include <rtdevice.h>
#include "rtdef.h"
#include "rttypes.h"
#include "referee.h"
#include "referee_crc.h"

#define LOG_TAG              "referee"
#define LOG_LVL              LOG_LVL_DBG
#include <ulog.h>

#define REFEREE_UART_NAME       "uart6"      /* 串口设备名称 */


/* 串口设备句柄 */
rt_device_t referee_serial;
/* 初始化配置参数 */
struct serial_configure referee_config = RT_SERIAL_CONFIG_DEFAULT;  
/* 指向信号量的指针 */
rt_sem_t referee_sem = RT_NULL;

static referee_info_t referee_info;			  // 裁判系统数据

/**
 * @brief  读取裁判数据,中断中读取保证速度
 * @param  buff: 读取到的裁判系统原始数据
 * @retval 是否对正误判断做处理
 * @attention  在此判断帧头和CRC校验,无误再写入数据，不重复判断帧头
 */
static void JudgeReadData(rt_uint8_t *buff)
{
    uint16_t judge_length; // 统计一帧数据长度
	if (buff == RT_NULL)	   // 空数据包，则不作任何处理
        return;

	// 写入帧头数据(5-byte),用于判断是否开始存储裁判数据
	rt_memcpy(&referee_info.FrameHeader, buff, LEN_HEADER);

	// 判断帧头数据(0)是否为0xA5
	if (buff[SOF] == REFEREE_SOF)
	{
		// 帧头CRC8校验
		if (Verify_CRC8_Check_Sum(buff, LEN_HEADER) == TRUE)
		{
			// 统计一帧数据长度(byte),用于CR16校验
			judge_length = buff[DATA_LENGTH] + LEN_HEADER + LEN_CMDID + LEN_TAIL;
			// 帧尾CRC16校验
			if (Verify_CRC16_Check_Sum(buff, judge_length) == TRUE)
			{
				// 2个8位拼成16位int
				referee_info.CmdID = (buff[6] << 8 | buff[5]);
				// 解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度)
				// 第8个字节开始才是数据 data=7
				switch (referee_info.CmdID)
				{
				case ID_game_state: // 0x0001
					rt_memcpy(&referee_info.GameState, (buff + DATA_Offset), LEN_game_state);
					break;
				case ID_game_result: // 0x0002
					rt_memcpy(&referee_info.GameResult, (buff + DATA_Offset), LEN_game_result);
					break;
				case ID_game_robot_survivors: // 0x0003
					rt_memcpy(&referee_info.GameRobotHP, (buff + DATA_Offset), LEN_game_robot_HP);
					break;
				case ID_event_data: // 0x0101
					rt_memcpy(&referee_info.EventData, (buff + DATA_Offset), LEN_event_data);
					break;
				case ID_supply_projectile_action: // 0x0102
					rt_memcpy(&referee_info.SupplyProjectileAction, (buff + DATA_Offset), LEN_supply_projectile_action);
					break;
				case ID_game_robot_state: // 0x0201
					rt_memcpy(&referee_info.GameRobotState, (buff + DATA_Offset), LEN_game_robot_state);
					break;
				case ID_power_heat_data: // 0x0202
					rt_memcpy(&referee_info.PowerHeatData, (buff + DATA_Offset), LEN_power_heat_data);
					break;
				case ID_game_robot_pos: // 0x0203
					rt_memcpy(&referee_info.GameRobotPos, (buff + DATA_Offset), LEN_game_robot_pos);
					break;
				case ID_buff_musk: // 0x0204
					rt_memcpy(&referee_info.BuffMusk, (buff + DATA_Offset), LEN_buff_musk);
					break;
				case ID_aerial_robot_energy: // 0x0205
					rt_memcpy(&referee_info.AerialRobotEnergy, (buff + DATA_Offset), LEN_aerial_robot_energy);
					break;
				case ID_robot_hurt: // 0x0206
					rt_memcpy(&referee_info.RobotHurt, (buff + DATA_Offset), LEN_robot_hurt);
					break;
				case ID_shoot_data: // 0x0207
					rt_memcpy(&referee_info.ShootData, (buff + DATA_Offset), LEN_shoot_data);
					break;
				case ID_student_interactive: // 0x0301   syhtodo接收代码未测试
					rt_memcpy(&referee_info.ReceiveData, (buff + DATA_Offset), LEN_receive_data);
					break;
				}
			}
		}
		// 首地址加帧长度,指向CRC16下一字节,用来判断是否为0xA5,从而判断一个数据包是否有多帧数据
		if (*(buff + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL) == 0xA5)
		{ // 如果一个数据包出现了多帧数据,则再次调用解析函数,直到所有数据包解析完毕
			JudgeReadData(buff + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL);
		}
	}
}

/*裁判系统串口接收回调函数,解析数据 */

static rt_err_t RefereeRxCallback(rt_device_t dev, rt_size_t size)
{
    rt_uint8_t buff[BSP_UART6_RX_BUFSIZE-1];

    rt_sem_release(referee_sem);

    return RT_EOK;
}

int referee_init(void)
{
    rt_err_t ret = RT_EOK;

    /* 查找串口设备 */
    referee_serial = rt_device_find(REFEREE_UART_NAME);
    if (!referee_serial)
    {
        LOG_E("find %s failed!\n", REFEREE_UART_NAME);
        return RT_ERROR;
    }

	/* 创建一个动态信号量，初始值是 0 */
    referee_sem = rt_sem_create("referee_sem", 0, RT_IPC_FLAG_PRIO);
    if (referee_sem == RT_NULL)
    {
        LOG_E("create dynamic semaphore failed.\n");
        return -1;
    }

    referee_config.rx_bufsz = BSP_UART6_RX_BUFSIZE-1;
    referee_config.tx_bufsz = BSP_UART6_TX_BUFSIZE;
    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(referee_serial, RT_DEVICE_CTRL_CONFIG, &referee_config);

    /* 以 DMA 接收及轮询发送方式打开串口设备 */
    rt_device_open(referee_serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(referee_serial, RefereeRxCallback);



	LOG_D("referee uart init sucess\n");

    return ret;
}
//INIT_APP_EXPORT(referee_init);


/**
 * @brief 裁判系统数据发送函数
 * @param
 */
void RefereeSend(uint8_t *send, uint16_t tx_len)
{
	rt_device_read(referee_serial, -1,send, tx_len);
	rt_thread_mdelay(100);
}
